package com.atguigu.day12.test08;

import java.util.Arrays;

public class MyArrayList implements Touchable {
    private Object[] all = new Object[2];
    private int total;
    Selector m ;

    public Object[] getAll() {
        return all;
    }

    public int getTotal() {
        return total;
    }

    public void add(Object element) {
        if (total < all.length) {
            all[total] = element;
            total++;
        } else if (total == all.length) {
            ex(all);
            all[total] = element;
            total++;
        }
    }

    public Object[] ex(Object[] all) {
        int x = all.length * 2;
        Object[] temp = all;
        this.all = new Object[x];
        for (int i = 0; i < total; i++) {
            this.all[i] = temp[i];
        }
        return this.all;
    }

    public void remove(int index) {
        if (index < 0 || index >= total) {
            System.out.println("没有这个元素");
            return;
        } else {
            all[index] = null;
            total--;
            for (int i = index; i < total; i++) {
                all[index]=all[index+1];
            }
            return;
        }
    }

    public void set(int index, Object value) {
        if (index < 0 || index >= total) {
            System.out.println("没有这个元素");
            return;
        } else {
            all[index] = value;
            return;
        }
    }

    public Object get(int index) {
        if (index < 0 || index >= total) {
            System.out.println("没有这个元素");
            return null;
        } else {
            return all[index];
        }
    }
    @Override
    public Selector select() {
        m = new MySelector();
        return m;
    }
    //    public class MyArrayList implements Touchable{
    private class MySelector implements Selector {
        int cursor;

        @Override
        public boolean nasNext() {
            return cursor != total;
        }

        @Override
        public Object next() {
            return all[cursor++];
        }
    }



//    public boolean select1(){
//        Selector m = new MySelector();
//        if (m.equals(null))
//            return false;
//        else
//            return true;
//    }
}
